<script setup>
import { useUserStore } from '@/stores'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { upimage, userMessagechange, userRegistercode } from '/src/api/user.js'
import { Plus, Upload } from '@element-plus/icons-vue'
const userStore = useUserStore()
const router = useRouter()
const {
  user: { name, username, idCard, phone, image, sexs }
} = useUserStore()
const formRef = ref()
const totalSecond = ref(60)
const second = ref(60)
const timer = ref(null)
const userInfo = ref({
  name,
  username,
  password: null,
  idCard,
  phone,
  image,
  sexs,
  code: ''
})
const rules = {
  name: [{ required: true, message: '请输入昵称', trigger: 'change' }],
  username: [{ required: true, message: '请输入用户名', trigger: 'change' }],
  password: [
    { required: false, message: '请输入密码', trigger: 'change' },
    {
      pattern: /^\S{1,15}$/,
      message: '密码必须是1—15位的非空字符',
      trigger: 'change'
    }
  ],
  sexs: [{ required: true, message: '请输入性别', trigger: 'change' }],
  idCard: [{ required: true, message: '请输入学号', trigger: 'change' }],
  phone: [
    { required: true, message: '请输入电话', trigger: 'change' },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: '电话格式不正确',
      trigger: 'change'
    }
  ],
  code: [{ required: true, message: '请输入验证码', trigger: 'change' }]
}
const onimage = async () => {
  const res = await upimage(fileToUpload.value)
  console.log(res)
  userInfo.value.image = res.data
  await userStore.getUser()
}
const changeusermessage = async () => {
  await formRef.value.validate()
  console.log(userInfo.value)
  const res = await userMessagechange(userInfo.value)
  await userStore.getUser()
  router.push('/user/change')
  location.reload()
  if (res.code == 1) {
    ElMessage.success(res.data)
  } else {
    ElMessage.error(res.data)
  }
}
const uploadRef = ref()
const fileToUpload = ref()
const imgUrl = ref(userStore.user.image)
const onUploadFile = (file) => {
  const reader = new FileReader()
  reader.readAsDataURL(file.raw)
  reader.onload = () => {
    imgUrl.value = reader.result
  }
  fileToUpload.value = file.raw
  console.log(fileToUpload.value)
  onimage()
}
const getcode = async () => {
  // ElMessage.success('发送成功')
  if (!timer.value && second.value === totalSecond.value) {
    timer.value = setInterval(() => {
      second.value--
      if (second.value < 1) {
        clearInterval(timer.value)
        timer.value = null
        second.value = totalSecond.value
      }
    }, 1000)
    console.log(userStore.user.email)
    const res = await userRegistercode({ email: userStore.user.email })
    if (res.code == 1) {
      ElMessage.success(res.data)
    } else {
      ElMessage.error(res.data)
    }
  }
}
const options = [
  { value: '男', label: '我是男生' },
  { value: '女', label: '我是女生' },
  { value: '未知', label: '这是私密' }
]
</script>
<template>
  <div class="user-change-container">
    <div class="page-header">
      <h2>个人信息修改</h2>
      <div class="header-line"></div>
    </div>

    <el-row class="main-content">
      <!-- 头像上传区域 -->
      <el-col :xs="24" :sm="8" class="avatar-section">
        <div class="upload-container">
          <el-upload
            ref="uploadRef"
            class="avatar-uploader"
            :auto-upload="false"
            :show-file-list="false"
            :on-change="onUploadFile"
          >
            <div class="avatar-wrapper">
              <img v-if="imgUrl" :src="imgUrl" class="avatar" />
              <img v-else src="@/assets/avatar.png" class="avatar" />
              <div class="hover-mask">
                <el-icon><Camera /></el-icon>
                <span>更换头像</span>
              </div>
            </div>
          </el-upload>

          <div class="upload-buttons">
            <el-button
              @click="uploadRef.$el.querySelector('input').click()"
              type="primary"
              :icon="Plus"
              size="large"
              class="btn-choose"
              >选择图片</el-button
            >
            <el-button
              type="success"
              :icon="Upload"
              size="large"
              @click="onimage"
              class="btn-upload"
              >上传头像</el-button
            >
          </div>
        </div>
      </el-col>

      <!-- 表单区域 -->
      <el-col :xs="24" :sm="16" class="form-section">
        <el-form
          :model="userInfo"
          :rules="rules"
          ref="formRef"
          label-width="120px"
          size="large"
          class="change-form"
        >
          <el-form-item label="登录名称" prop="username">
            <el-input
              v-model="userInfo.username"
              class="custom-input"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户昵称" prop="name">
            <el-input v-model="userInfo.name" class="custom-input"></el-input>
          </el-form-item>
          <el-form-item label="用户新密码" prop="password">
            <el-input
              v-model="userInfo.password"
              type="password"
              class="custom-input"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户学号" prop="idCard">
            <el-input v-model="userInfo.idCard" class="custom-input"></el-input>
          </el-form-item>
          <el-form-item label="用户性别" prop="sexs">
            <el-select
              v-model="userInfo.sexs"
              placeholder="请选择性别"
              class="custom-select"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="用户电话" prop="phone">
            <el-input v-model="userInfo.phone" class="custom-input"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code" class="code-item">
            <el-input
              v-model="userInfo.code"
              placeholder="发送到邮箱"
              class="code-input"
            ></el-input>
            <el-button
              @click="getcode"
              type="primary"
              class="code-button"
              :disabled="second !== totalSecond"
            >
              {{ second === totalSecond ? '发送验证码' : `${second}秒后重试` }}
            </el-button>
          </el-form-item>
          <el-form-item class="submit-item">
            <el-button
              type="primary"
              @click="changeusermessage"
              class="submit-button"
              >保存修改</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style lang="scss" scoped>
.user-change-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;

  .page-header {
    text-align: center;
    margin-bottom: 40px;

    h2 {
      color: #333;
      font-size: 28px;
      margin-bottom: 15px;
    }

    .header-line {
      width: 60px;
      height: 3px;
      background: var(--el-color-primary);
      margin: 0 auto;
      border-radius: 2px;
    }
  }

  .main-content {
    background: white;
    border-radius: 15px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    padding: 30px;
  }

  .avatar-section {
    .upload-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20px;

      .avatar-wrapper {
        position: relative;
        width: 200px;
        height: 200px;
        border-radius: 50%;
        overflow: hidden;
        margin-bottom: 20px;
        cursor: pointer;
        transition: transform 0.3s ease;

        &:hover {
          transform: scale(1.02);

          .hover-mask {
            opacity: 1;
          }
        }

        .avatar {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }

        .hover-mask {
          position: absolute;
          top: 0;
          left: 0;
          width: 100%;
          height: 100%;
          background: rgba(0, 0, 0, 0.5);
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          opacity: 0;
          transition: opacity 0.3s ease;

          .el-icon {
            font-size: 30px;
            color: white;
            margin-bottom: 10px;
          }

          span {
            color: white;
            font-size: 14px;
          }
        }
      }

      .upload-buttons {
        display: flex;
        gap: 10px;

        .el-button {
          transition: transform 0.2s ease;

          &:hover {
            transform: translateY(-2px);
          }
        }
      }
    }
  }

  .form-section {
    .change-form {
      max-width: 600px;
      margin: 0 auto;

      .el-form-item {
        margin-bottom: 25px;
        transition: transform 0.2s ease;

        &:hover {
          transform: translateX(5px);
        }
      }

      .custom-input,
      .custom-select {
        .el-input__wrapper {
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
          border-radius: 8px;

          &:hover {
            box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
          }
        }
      }

      .code-item {
        display: flex;

        .code-input {
          flex: 1;
          margin-right: 10px;
        }

        .code-button {
          width: 120px;
          transition: all 0.3s ease;
        }
      }

      .submit-item {
        text-align: center;
        margin-top: 40px;

        .submit-button {
          width: 180px;
          height: 45px;
          font-size: 16px;
          transition: all 0.3s ease;

          &:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(var(--el-color-primary-rgb), 0.4);
          }
        }
      }
    }
  }
}

// 移动端适配
@media screen and (max-width: 768px) {
  .user-change-container {
    padding: 10px;

    .main-content {
      padding: 15px;
    }

    .avatar-section {
      margin-bottom: 30px;
    }

    .form-section {
      .change-form {
        .el-form-item {
          margin-bottom: 20px;
        }
      }
    }
  }
}
</style>
